<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary" size="small" @click.stop="MySumbit"
            v-if="false"
            v-permission="permissionList.submitBtn">提交</el-button>
          <el-popover
            v-if="permissionList.auditBtn"
            placement="bottom"
            width="260"
            v-model="batchReviewVisible"
          >
            <div style="text-align: center; margin: 0">
              <el-form>
                <el-form-item label="审批建议">
                  <el-input type="textarea" span="24" v-model="checkReason"></el-input>
                </el-form-item>
              </el-form>
               <el-row>
              <el-button size="mini" type="primary" @click="batchReview(2)">审批通过</el-button>
              <el-button type="primary" size="mini" @click="batchReview(3)">审批不通过</el-button>
               </el-row>
              <el-row style="margin-top:5px;">
                <el-button type="primary" size="mini" @click="batchReviewVisible=false">取消</el-button>
              </el-row>
            </div>
            <el-button type="primary" size="small" slot="reference">审批</el-button>
          </el-popover>
          <el-button type="primary" size="small"
            v-permission="permissionList.submitBatchBtn" @click.stop="handleBatchSubmit">提交</el-button>
          <el-button type="primary" size="small"
            v-permission="permissionList.batchDelBtn" @click.stop="MyDel">删除</el-button>

          <el-button type="primary" size="small" v-permission="permissionList.exportBtn"
            @click.stop="handleExport">导 出</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small"
            icon="el-icon-edit"
            v-permission="permissionList.editBtn"
            v-if="scope.row.checkStatus=='0'&&scope.row.ticketSource=='手动新增'"
            @click="rowEdit(scope.row, scope.index)">
            编 辑
          </el-button>
          <!-- <el-button type="text" size="small"
            icon="el-icon-delete"
            v-if="scope.row.checkStatus=='0'"
            @click="rowDel(scope.row, scope.index)">
            删 除
          </el-button> -->
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  addObj,
  delObj,
  fetchList,
  putObj,
  exportExcel,
  submitObj,
  submitBatch,
  auditObj,
  delsObj
} from "@/api/subway/swstinvalidticketproduce";

export default {
  name: "SwStInvalidTicketProduce",
  mixins: [baseCrud('subway/swstinvalidticketproduce')],
  data() {
    return {
      exportName: '无效票生产申请信息列表.xlsx',
      dialogFormVisible: false,
      batchReviewVisible: false,
      checkReason:'',
      searchForm: {
        checkStatus: '1'
      }
    };
  },
  watch: {
    "form.ticketTimesRemain": {
      handler(val) {
        var ticketPriceRemain = this.findObject(
          this.tableOption.column,
          "ticketPriceRemain"
        );
        if (val === "") {
          ticketPriceRemain.disabled = false;
          ticketPriceRemain.rules = [
            {
              message: "请输入票卡剩余金额",
              trigger: "blur",
            },
          ];
        } else {
          ticketPriceRemain.disabled = true;
          ticketPriceRemain.rules = [];
        }
      },
    },
    "form.ticketPriceRemain": {
      handler(val) {
        var ticketTimesRemain = this.findObject(
          this.tableOption.column,
          "ticketTimesRemain"
        );
        if (val === "") {
          ticketTimesRemain.disabled = false;
          ticketTimesRemain.rules = [
            {
              message: "请输入票卡剩余次数",
              trigger: "blur",
            },
          ];
        } else {
          ticketTimesRemain.disabled = true;
          ticketTimesRemain.rules = [];
        }
      },
    },
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_add, false),
        delBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_delete, false),
        batchDelBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_deletes, false),
        editBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_update, false),
        submitBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_submit, false),
        auditBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_audit, false),
        exportBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_export, false),
        submitBatchBtn: this.vaildData(this.permissions.st_invalid_ticket_produce_submitBatch, false)
      };
    },
  },
  created(){
    if(this.permissionList.auditBtn){
      this.tableOption.menu = false
    }
  },
  methods: {
    handleBatchSubmit(){
      var tableSelect = this.$refs.crud.tableSelect
      if(tableSelect.length == 0){
        this.$message.info('请选择要提交的记录')
        return
      }
      // let ids=[];
      // for (const table of tableSelect) {
      //   ids.push(table.id);
      // }
      submitBatch(tableSelect).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    beforeRowSave(row, callback, loading){
      row.checkStatus = '0'
      callback()
    },
     MyDel(status) {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要删除的数据");
        return;
      }
      let ids=[];
      for (const table of tableSelect) {
        if(table.checkStatus!=0){
          this.$message.info("只能删除未提交的数据！");
          return;
        }
        ids.push(table.id);
      }
      delsObj(ids).then((data) => {
        this.$message.success("删除成功");
        this.getList(this.page);
      });
    },
     MySumbit() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要提交的数据");
        return;
      }
      submitObj(tableSelect).then((data) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    batchReview(status) {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审批的数据");
        return;
      }
      for (const table of tableSelect) {
        if(table.submitStatus!=1){
          this.$message.info("只能审批已提交的数据！");
          return;
        }
        table.checkStatus = status;
        table.checkReason=this.checkReason;
      }
      auditObj(tableSelect).then((data) => {
        this.$message.success("审批成功");
        this.getList(this.page);
        this.batchReviewVisible = false;
      });
    },
    handleBeforeOpen(done) {
      this.form.type = this.dictType;
      this.form.dictId = this.dictId;
      done();
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
